package com.gxuwz.prrs.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.*;
import android.media.Image;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.gxuwz.prrs.R;
import com.gxuwz.prrs.bean.LoginForm;
import com.gxuwz.prrs.bean.UserInfo;
import com.gxuwz.prrs.service.ServiceHolder;
import com.gxuwz.prrs.service.impl.AuthServiceImpl;
import com.gxuwz.prrs.utils.BaseApplication;
import com.gxuwz.prrs.utils.NavigationUtils;
import com.gxuwz.prrs.utils.UpdateUtils;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @author 张钧
 * 用户-我的页面
 */
public class MineActivity extends AppCompatActivity {
	ImageView avatar;
	TextView name;
	TextView phone;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_mine);
		NavigationUtils.setRedirectBottom(this);
		name = findViewById(R.id.name);
		phone = findViewById(R.id.phone);
		avatar = findViewById(R.id.avatar);
		loadMinePage();
	}

	private void loadMinePage(){
		try {
			CompletableFuture.runAsync(()->{
				AuthServiceImpl authService = ServiceHolder.getInstance().getBean(AuthServiceImpl.class);
				UserInfo userInfo = authService.getUserInfo();
				if(userInfo != null){
					name.setText(userInfo.getName());
					phone.setText(userInfo.getPhone());
					requestImg(userInfo.getAvatar());
				}
				ImageView changePassword = findViewById(R.id.change_password);
				ImageView showRecords = findViewById(R.id.show_records);
				ImageView checkUpdate = findViewById(R.id.check_update);
				ImageView logout = findViewById(R.id.logout);
				changePassword.setOnClickListener(view -> {
					NavigationUtils.redirectToChangePasswordActivity(MineActivity.this,null);
				});
				showRecords.setOnClickListener(view -> {
					NavigationUtils.redirectToAppointmentRecordsActivity(MineActivity.this,null);
				});
				checkUpdate.setOnClickListener(view -> UpdateUtils.update(getVersionCode(MineActivity.this),MineActivity.this));
				logout.setOnClickListener(view -> {
					SharedPreferences sharedPreferences = this.getSharedPreferences("token", Context.MODE_PRIVATE);
					SharedPreferences.Editor edit = sharedPreferences.edit();
					edit.remove ("token");
					edit.apply();
					Toast.makeText(this, "登出成功", Toast.LENGTH_SHORT).show();
					NavigationUtils.redirectToLoginActivity(this,null);
				});
			}).get();
		} catch (ExecutionException | InterruptedException e) {
			throw new RuntimeException(e);
		}
	}

	public String getVersionCode(Context context) {
		try {
			String pkName = context.getPackageName();
			return context.getPackageManager().getPackageInfo(
					pkName, 0).versionName;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	private void requestImg(String avatar) {
		URL url;
		try {
			url = new URL(avatar);
		} catch (MalformedURLException e) {
			throw new RuntimeException(e);
		}
		Bitmap bitmap;
		try {
			bitmap = createCircleImage(CompletableFuture.supplyAsync(() -> {
				try {
					return BitmapFactory.decodeStream(url.openStream());
				} catch (IOException e) {
					e.printStackTrace();
				}
				return null;
			}).get());
		} catch (ExecutionException | InterruptedException e) {
			throw new RuntimeException(e);
		}
		if (bitmap != null) {
			this.avatar.setImageBitmap(bitmap);
		}
	}

	public static Bitmap createCircleImage(Bitmap source) {
		int length = Math.min(source.getWidth(), source.getHeight());
		Paint paint = new Paint();
		paint.setAntiAlias(true);
		Bitmap target = Bitmap.createBitmap(length, length, Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(target);
		canvas.drawCircle(length / 2, length / 2, length / 2, paint);
		paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
		canvas.drawBitmap(source, 0, 0, paint);
		return target;
	}
}
